﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Device;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Device
{
    public class MachineryStatusServices: IMachineryStatus
    {
        public IBaseRepository Db { get; }

        public MachineryStatusServices(IBaseRepository db)
        {
            Db = db;
        }

        public async Task<IEnumerable<MachineryStatus>> GetAllAsync()
        {
            string sql = "select * from MachineryStatus";
            var list = await Db.QueryAllAsync<MachineryStatus>(sql);
            return list;
        }

        public async Task<IEnumerable<MachineryStatus>> GetAsync(int PageIndex, int PageSize,string StatusName)
        {
            string sql = "select * from MachineryStatus where 1=1 ";
    
            if (!string.IsNullOrEmpty(StatusName))
            {
                sql += " and StatusName like @StatusName ";
            }

            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";



            var list = await Db.QueryAsync<MachineryStatus>(sql, new { PageIndex, PageSize, StatusName = $"%{StatusName}%" });
            return list;
        }

        public async Task<int> GetCountAsync( string StatusName)
        {
            string sql = "select count(*) from MachineryStatus where 1=1 ";
     

            if (!string.IsNullOrEmpty(StatusName))
            {
                sql += " and StatusName like @StatusName ";
            }

            var count = await Db.GetRecoreCountAsync(sql, new { StatusName = $"%{StatusName}%" });
            return count;
        }

        public async Task<bool> UpdateAsync(MachineryStatus model)
        {
            var result = await Db.UpdateAsync(model);
            return result;
        }

        public async Task<bool> AddAsync(MachineryStatus model)
        {

            var result = await Db.InsertAsync(model);
            return result;
        }

        public async Task<bool> DeleteAsync(int[] Ids)
        {

            string sql = "delete MachineryStatus where Id in @Ids";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }

        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete MachineryStatus where Id = @Id";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }


    }
}
